#include "CharacterDataDelegate.h"
#include <Item.h>


#include <QDebug>
#include <QComboBox>


CharacterDataDelegate::CharacterDataDelegate(D20CharacterModel *sourceModel, QObject *parent)
    : QItemDelegate(parent)
{
    m_model = sourceModel;
}



QWidget* CharacterDataDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
//    qDebug() << Q_FUNC_INFO << "A";
    if (!index.isValid() || index.column() != Item::ValueData) return QItemDelegate::createEditor(parent, option, index);

//    qDebug() << Q_FUNC_INFO << "B";
    if (m_model.isNull()) return QItemDelegate::createEditor(parent, option, index);

    QModelIndex groupIndex = index;
    while (true)
    {
        if (!groupIndex.parent().isValid()) break;
        groupIndex = groupIndex.parent();
    }

//    qDebug() << Q_FUNC_INFO << "C";

    groupIndex = groupIndex.sibling(groupIndex.row(), AbstractItem::IdData);
    QModelIndex itemIndex = index.sibling(index.row(), AbstractItem::IdData);

    QString groupId = groupIndex.data(Qt::EditRole).toString();
    QString itemId = itemIndex.data(Qt::EditRole).toString();

//    qDebug() << Q_FUNC_INFO << groupId << itemId;
    if (groupId.endsWith(m_model->groupName(D20CharacterModel::HitDie)) ||
        groupId.endsWith(m_model->groupName(D20CharacterModel::SkillPoints)))
    {
        return new QComboBox(parent);
    }

    if (groupId.endsWith(m_model->groupName(D20CharacterModel::BaseBonuses)) ||
        groupId.endsWith(m_model->groupName(D20CharacterModel::SavingThrows)))
    {
        if (groupId == itemId) return QItemDelegate::createEditor(parent, option, index);
        return new QComboBox(parent);
    }


    return QItemDelegate::createEditor(parent, option, index);
}



void CharacterDataDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
//    qDebug() << Q_FUNC_INFO << "A";
    QComboBox* cb = qobject_cast<QComboBox*>(editor);
    if (!cb) return QItemDelegate::setEditorData(editor, index);

//    qDebug() << Q_FUNC_INFO << "B";
    if (m_model.isNull()) return QItemDelegate::setEditorData(editor, index);

    QModelIndex groupIndex = index;
    while (true)
    {
        if (!groupIndex.parent().isValid()) break;
        groupIndex = groupIndex.parent();
    }

    groupIndex = groupIndex.sibling(groupIndex.row(), AbstractItem::NameData);
    QModelIndex itemIndex = index.sibling(index.row(), AbstractItem::NameData);

    QString groupName = groupIndex.data(Qt::EditRole).toString();
    QString itemName = itemIndex.data(Qt::EditRole).toString();

//    qDebug() << Q_FUNC_INFO << "C";
    QVariantMap options = m_model->options(groupName, itemName);
    if (options.isEmpty()) return QItemDelegate::setEditorData(editor, index);

//    qDebug() << Q_FUNC_INFO << "D";
    foreach (QString name, options.keys())
    {
        QVariant value = options.value(name);
        if (name == value.toString())
        {
            cb->addItem(name, value);
            continue;
        }

        cb->addItem(QString("%1 (%2)").arg(name).arg(value.toString()), value);
    }
}



void CharacterDataDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{

}
